package com.fancy.mobileschool.util;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * Created by l on 2017/11/22.
 */

public class LocationHelper implements AMapLocationListener {
    public static double broder_west=120.339144;
    public static double broder_east=120.348044;
    public static double broder_south=30.311215;
    public static double broder_north=30.315740;
    public double latitude;//经度
    public double longitude;//纬度


    private  AMapLocationClient mLocationClient = null;//定位发起端
    private  AMapLocationClientOption mLocationOption = null;//定位参数


    public LocationHelper(Context context) {
        mLocationClient = new AMapLocationClient(context);
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(true);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动
        mLocationClient.startLocation();

    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {

                latitude=amapLocation.getLatitude();
                longitude=amapLocation.getLongitude();

            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }
        }
    }

    public double getLatitude() {
        return latitude;
    }



    public double getLongitude() {
        return longitude;
    }


    public boolean isEnter(){


        //判断
        if(latitude>=broder_south&&latitude<=broder_north&&longitude<=broder_east&&longitude>=broder_south){
            return true;
        }

        return false;
    }


    public void close(){
        mLocationClient.stopLocation();
        mLocationClient.onDestroy();
    }
}
